<template>
  <view class="address-page">
    <!-- 自定义导航栏 -->
    <nav-bar title="地址管理" :show-back="true"></nav-bar>
    
    <!-- 地址列表 -->
    <view class="address-list" v-if="addressList.length > 0">
      <view 
        class="address-item"
        v-for="(address, index) in addressList"
        :key="address.id"
      >
        <view class="address-content">
          <view class="address-info">
            <view class="info-row">
              <text class="name">{{ address.name }}</text>
              <text class="phone">{{ address.phone }}</text>
            </view>
            <view class="address-text">{{ formatAddress(address) }}</view>
          </view>
          <view class="address-action">
            <view class="default-tag" v-if="address.isDefault">默认</view>
            <view class="edit-btn" @click.stop="editAddress(address)">编辑</view>
          </view>
        </view>
        <view class="address-bottom">
          <view class="default-switch">
            <switch 
              :checked="address.isDefault" 
              color="#32C5FF" 
              @change="(e) => setDefault(address, e.detail.value)"
            />
            <text>设为默认地址</text>
          </view>
          <view class="delete-btn" @click.stop="confirmDelete(address, index)">删除</view>
        </view>
      </view>
    </view>
    
    <!-- 无地址状态 -->
    <view v-else class="empty-address">
      <image src="/static/images/empty-address.png" mode="aspectFit" class="empty-image"></image>
      <text class="empty-text">暂无收货地址</text>
    </view>
    
    <!-- 添加地址按钮 -->
    <view class="add-address">
      <button class="add-btn" @click="addAddress">新增收货地址</button>
    </view>
  </view>
</template>

<script setup>
import { ref, onMounted } from 'vue';
import NavBar from '@/components/navbar/NavBar.vue';

// 地址列表
const addressList = ref([]);

// 格式化完整地址
const formatAddress = (address) => {
  return `${address.province}${address.city}${address.district}${address.detail}`;
};

// 设置默认地址
const setDefault = (address, value) => {
  if (value) {
    // 取消其他默认
    addressList.value.forEach(item => {
      item.isDefault = item.id === address.id;
    });
    
    // 实际项目中应该调用API更新默认地址
    uni.showToast({
      title: '设置默认地址成功',
      icon: 'success'
    });
  } else {
    // 不允许取消默认状态
    address.isDefault = true;
    
    uni.showToast({
      title: '至少需要一个默认地址',
      icon: 'none'
    });
  }
};

// 编辑地址
const editAddress = (address) => {
  uni.navigateTo({
    url: `/pages/user/address-edit?id=${address.id}`
  });
};

// 添加地址
const addAddress = () => {
  uni.navigateTo({
    url: '/pages/user/address-edit'
  });
};

// 确认删除
const confirmDelete = (address, index) => {
  uni.showModal({
    title: '提示',
    content: '确定要删除此地址吗？',
    success: function(res) {
      if (res.confirm) {
        deleteAddress(address, index);
      }
    }
  });
};

// 删除地址
const deleteAddress = (address, index) => {
  // 实际项目中应该调用API删除地址
  addressList.value.splice(index, 1);
  
  uni.showToast({
    title: '删除成功',
    icon: 'success'
  });
};

// 初始化地址列表
const initAddressList = () => {
  // 模拟数据
  addressList.value = [
    {
      id: 1,
      name: '张三',
      phone: '13800138000',
      province: '广东省',
      city: '广州市',
      district: '海珠区',
      detail: '新港中路397号',
      isDefault: true
    },
    {
      id: 2,
      name: '李四',
      phone: '13900139000',
      province: '广东省',
      city: '广州市',
      district: '天河区',
      detail: '天河路385号',
      isDefault: false
    }
  ];
};

onMounted(() => {
  // 初始化地址列表
  initAddressList();
});
</script>

<style lang="scss" scoped>
.address-page {
  min-height: 100vh;
  background-color: #f8f8f8;
  padding-bottom: 180rpx;
}

.address-list {
  padding: 20rpx;
}

.address-item {
  background-color: #ffffff;
  border-radius: 12rpx;
  margin-bottom: 20rpx;
  overflow: hidden;
  box-shadow: 0 2rpx 6rpx rgba(0, 0, 0, 0.05);
}

.address-content {
  padding: 30rpx;
  display: flex;
  border-bottom: 1px solid #f5f5f5;
}

.address-info {
  flex: 1;
  
  .info-row {
    margin-bottom: 16rpx;
    
    .name {
      font-size: 32rpx;
      font-weight: bold;
      margin-right: 20rpx;
    }
    
    .phone {
      font-size: 28rpx;
      color: #666666;
    }
  }
  
  .address-text {
    font-size: 28rpx;
    color: #333333;
    line-height: 1.5;
  }
}

.address-action {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  justify-content: space-between;
  
  .default-tag {
    font-size: 24rpx;
    color: #32C5FF;
    background-color: rgba(50, 197, 255, 0.1);
    padding: 4rpx 12rpx;
    border-radius: 6rpx;
  }
  
  .edit-btn {
    font-size: 24rpx;
    color: #999999;
    margin-top: 10rpx;
  }
}

.address-bottom {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20rpx 30rpx;
  background-color: #ffffff;
  
  .default-switch {
    display: flex;
    align-items: center;
    
    text {
      font-size: 28rpx;
      color: #666666;
      margin-left: 10rpx;
    }
  }
  
  .delete-btn {
    font-size: 28rpx;
    color: #FF5252;
  }
}

.empty-address {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 100rpx 0;
  
  .empty-image {
    width: 240rpx;
    height: 240rpx;
    margin-bottom: 30rpx;
  }
  
  .empty-text {
    font-size: 28rpx;
    color: #999999;
  }
}

.add-address {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  padding: 30rpx;
  background-color: #ffffff;
  box-shadow: 0 -2rpx 10rpx rgba(0, 0, 0, 0.05);
  
  .add-btn {
    width: 100%;
    height: 90rpx;
    background: linear-gradient(135deg, #32C5FF, #BA68C8);
    color: #ffffff;
    border-radius: 45rpx;
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 32rpx;
    
    &::after {
      border: none;
    }
  }
}
</style> 